<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="rules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div>
        <img src="@/assets/img/logo.595745bd.png" alt="" class="login-logo">
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          name="username"
          type="text"
          placeholder="请输入账号"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.password"
          name="password"
          placeholder="请输入密码"
          show-password
        />
        <span class="show-pwd" />
      </el-form-item>

      <el-form-item prop="code">
        <div class="codedataipt">
          <el-input
            v-model="loginForm.code"
            prefix-icon="el-icon-price-tag"
            placeholder="请输入验证码"
            style="width: 100%"
          >
            <!-- 插槽把验证码插进来 要在el标签里面使用 基地址＋ 接口地址 + -->
            <template #suffix>
              <img
                :src="`http://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${clientToken}`"
                alt=""
                @click="getNewCode"
              >
            </template>

          </el-input>
        </div>
        <div />

      </el-form-item>

      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px; background-color: #6b7def"
        @click="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
import nanoid from '@/utils/getRandom'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: process.env.NODE_ENV === 'development' ? 'admin' : '',
        password: process.env.NODE_ENV === 'development' ? 'admin' : '',
        // mobile: '188814662390',
        code: '',
        loginType: 0
      },
      clientToken: '',

      rules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '请输入验证码' }
        ]
      }
    }
  },
  async created() {
    /* 获取数据的时候就调用二维码 */
    this.getImgCode()
  },
  methods: {
    /* 表单校验 */
    async handleLogin() {
      await this.$refs.loginForm.validate()
      // 异步  校验结束发请求  触发store.user的函数 调用接口  再跳转页面
      await this.$store.dispatch('user/login', {
        ...this.loginForm, clientToken: this.clientToken }

      )
      console.log(process.env)
      //   await 成功后面的代码才会执行 登录成功再跳转页面
      this.$router.push('/')
    },
    // 封装随机数的方法
    getImgCode() {
      const res = nanoid()
      this.clientToken = res
    },
    // 点击刷新二维码
    getNewCode() {
      this.getImgCode()
    }
  }
}
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .codedataipt{
    height: 50px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;

  //这里图片不显示加了~
  background-image: url("~@/assets/img/background.be4fae7d.png");
  background-repeat: no-repeat;
  background-size: cover;
  overflow: visible;
  .login-logo {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .el-form {

    //这里给表单大盒子加了溢出可见  不然是半圆
    overflow: visible;
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgba(30, 111, 72, 0.35);
    box-shadow: 0 3px 70px 0 rgba(30, 111, 72, 0.35);
    border-radius: 10px;

    ::v-deep.el-form-item__content {
      border-radius: 5px;
      background-color: #fff;
      .el-input__inner {
        border: 0px;

      }
      .el-input--prefix{
        width: 250px;
       }
    }
    .el-icon-price-tag{
      margin-right: 20px !important;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
 .codeimg{
 float: right;
 width: 130px;
 height: 50px;
 background-color: pink;
 }
}
</style>
